<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Manager as ManagerModel;
use app\common\model\ManagerJob as ManagerJobModel;
use app\common\model\ManagerDuty as ManagerDutyModel;
use app\common\model\Member as MemberModel;
use app\common\model\Renzhen as Renzhenmodel;
use app\common\model\Company as Companymodel;
use app\common\model\ManagerCompany as ManagerCompanymodel;
use app\common\model\ManagerPassword as ManagerPasswordModel;

/**
 * Manager 法师管理
 * Class PlayBeads
 * @package app\home\controller
 */
class Manager extends Base
{
    /**
     * [getIndex  获取寺院列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new ManagerModel;
        $cmodel = new CompanyModel;
        $mcmodel = new ManagerCompanymodel;
        $mem = $this->user;
        try{
            $manager = $model->findData($mem['tel'],'tel','id');
            $mcompany = $mcmodel->getAllData(['mid'=>$manager['id'],'status'=>1],'id','comid');
            foreach($mcompany as &$v){
                $company = $cmodel->findData($v['comid'],'id','id,comname,compic,states');
                $http = substr($company['compic'],0,4);
                if($http != 'http'){
                    $v['compic'] = config('domain_name').$company['compic'];
                }else{
                    $v['compic'] = $company['compic'];
                }
                $v['id'] = $company['id'];
                $v['comname'] = $company['comname'];
                $v['states'] = $company['states'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$mcompany);
    }
    /**
     * [postAdd  法师认证]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        $model = new ManagerModel();
        $rmodel = new RenzhenModel();

        if(!is_idcard($param['idcard']))
            return admin_json(-2,'身份证错误！');

        $tel = $model->getAllData(['tel'=>$mem['tel'],'states'=>['neq',3]]);
        if(!empty($tel))
            return admin_json(-3,'已注册法师！');

        $idcard = $model->getAllData(['idcard'=>$param['idcard'],'states'=>['neq',3]]);
        if(!empty($idcard))
            return admin_json(-4,'该身份证号已注册法师！');

        try{
            $renzhen = $rmodel->findData($mem['id'],'uid','id');
            $img = $param['zcard'].','.$param['fcard'].','.$param['jiedie'];
            if(empty($renzhen)){
                $data = array(
                    'type'=>1,
                    'idcard'=>$param['idcard'],
                    'fahao'=>$param['fahao'],
                    'siyuan'=>$param['siyuan'],
                    'card_pic'=>$img,
                    'addtime'=>date('Y-m-d H:i:s'),
                    'uid'=>$mem['id'],
                    'sex'=>$param['sex'],
                    'addr'=>$param['addr'],
                    'zongpai'=>$param['zongpai'],
                    'phone'=>$param['phone'],
                    'city'=>$param['city'],
                );
                $rmodel->addData($data);
            }else{
                $data = array(
                    'idcard'=>$param['idcard'],
                    'fahao'=>$param['fahao'],
                    'siyuan'=>$param['siyuan'],
                    'card_pic'=>$img,
                    'id'=>$renzhen['id'],
                    'status'=>0,
                    'sex'=>$param['sex'],
                    'addr'=>$param['addr'],
                    'zongpai'=>$param['zongpai'],
                    'phone'=>$param['phone'],
                    'city'=>$param['city']
                );
                $rmodel->editData($data);
            }

        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'添加失败'.$e->getMessage());
        }

        return admin_json(1, '添加成功');
    }
    /**
     * [getIsmanager  判断是否为法师]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIsshow()
    {
        $param = input('param.');
        $model = new ManagerModel();
//        $cmodel = new CompanyModel();
        $mem = $this->getMember($param['token']);
        $map = [];
        try{
            $map['states'] = ['neq',3];
            $map['tel'] = $mem['tel'];
            $res = $model->findOne($map,'id,tel');
            if(!empty($res)){
//                $company = $cmodel->findData($res['id'],'manager_id');
//                if(!empty($company)){
                    $isshow = 1;
//                }else{
//                    $isshow = 0;
//                }
            }else{
                $isshow = 0;
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',['isshow'=>$isshow]);
    }
    /**
     * [getRenzhen  获取法师认证]
     * @param
     * @return json
     * @author [C。]
     */
    public function getRenzhen()
    {
        $param = input('param.');
        $mem = $this->getMember($param['token']);
        $rmodel = new RenzhenModel();
        try{
            $renzhen = $rmodel->findData($mem['id'],'uid','idcard,fahao,siyuan,card_pic,status,sex,addr,zongpai,phone,city');
            $renzhen['card_pic'] = explode(',',$renzhen['card_pic']);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$renzhen);
    }
    /**
     * [getFashi  获取寺院常驻高僧]
     * @param
     * @return json
     * @author [C。]
     */
    public function getFashi()
    {
        $param = input('param.');
        $cmodel = new ManagerCompanymodel;
        try{
            $list = $cmodel->getByConditons(['mc.comid'=>$param['comid'],'mc.status'=>1],0,5);
            foreach($list as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }else{
                    $v['img'] = $v['img'];
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$list);
    }
    /**
     * [getFashiMore  获取寺院常驻更多高僧]
     * @param
     * @return json
     * @author [C。]
     */
    public function getFashiMore()
    {
        $param = input('param.');
        $cmodel = new ManagerCompanymodel;
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['mc.comid'] = $param['comid'];
            $map['mc.status'] = 1;
            $count = $cmodel->getByCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $cmodel->getByConditons($map, $this->from, $this->size);
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }else{
                    $v['img'] = $v['img'];
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  僧侣添加]
     * @param type 1 查询用户法师是否存在
     * @param ftype 2 法师不存在 3 法师已存在 4 用户不存在
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postAddmanager()
    {
        $param = input('param.');
        $map = [];
        $model = new ManagerModel;
        $memodel = new MemberModel;
        $mcmodel = new ManagerCompanyModel;
        $mpmodel = new ManagerPasswordModel;
        if(empty($param['img'])){
            $param['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/nouser.png';
        }
        $manager = $model->findData($param['tel'],'tel','id,tel,name,username');
        $member = $memodel->findData($param['tel'],'tel','id,sex,bathday,img');
        if(!empty($param['type'])){
            if($param['type'] == 1){
                if(!empty($member)){
                    $http = substr($member['img'],0,4);
                    if($http != 'http'){
                        $member['img'] = config('domain_name').$member['img'];
                    }
                    if(empty($manager)){
                        return admin_json(2,'法师不存在',$member);
                    }
                    $data = array(
                        'img' => $member['img'],
                        'name' => $manager['name'],
                        'username' => $manager['username'],
                        'tel' => $manager['tel'],
                        'sex' => $member['sex'],
                        'bathday' => $member['bathday'],
                    );
                    return admin_json(3,'法师已存在',$data);
                }else{
                    return admin_json(4,'用户不存在');
                }
            }
        }
        try{
            $pswd = $param['pswd'];
            if($param['ftype'] == 4){
                $param['addtime'] = time();
                $param['identity'] = 2;
                $param['pswd'] = md5( $param['pswd']);
                $member = $memodel->findData($param['tel'],'tel','id,sex,bathday,img');
                if(empty($member)){
                    $param['register_comid'] = $param['comid'];
                    $memodel->addData($param);
                }
            }else{
                $data = array();
                $data = array(
                    'identity' => 2,
                    'id' => $member['id'],
                );
                $memodel->editData($data);
                unset($param['id']);
            }

            if($param['ftype'] != 3){
                $manager = $model->findData($param['tel'],'tel','id,tel,name,username');
                if(empty($manager)){
                    $id = $model->addData($param);
                }
            }else{
                $id = $manager['id'];
                $model->editData(['id'=>$id,'name'=>$param['name']]);
            }

            $mc = $mcmodel->findOne(['comid'=>$param['comid'],'mid'=>$id]);
            if(empty($mc)){
                $data = array();
                $data = array(
                    'comid' => $param['comid'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                    'mid' => $id,
                    'addtime' => time()
                );
                $mcmodel->addData($data);
                $data = array();
                $data = array(
                    'comid' => $param['comid'],
                    'mid' => $id,
                    'password' => md5(md5($pswd) . config('admin.manager_password_halt')),
                    'addtime' => time(),
                    'pass' => $pswd,
                    'tel' => $param['tel'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                );
                if($param['dutyid'] == 1){
                    $data['states'] = -1;
                }
                $mpmodel->addData($data);
            }else{
                if($mc['status'] == 1){
                    return admin_json(-1,'法师已存在！');
                }
                $data = array();
                $data = array(
                    'id'=>$mc['id'],
                    'dutyid' => $param['dutyid'],
                    'jobid' => $param['jobid'],
                    'status'=>1
                );
                $mcmodel->editData($data);

                $mpass= $mpmodel->findOne(['comid'=>$param['comid'],'mid'=>$id]);
                if(!empty($mpass)){
                    $data = array();
                    $data = array(
                        'id'=>$mpass['id'],
                        'dutyid' => $param['dutyid'],
                        'jobid' => $param['jobid'],
                    );
                    if($param['dutyid'] == 1){
                        $data['states'] = -1;
                    }else{
                        $data['states'] = 1;
                    }
                    $mpmodel->editData($data);
                }else{
                    $data = array();
                    $data = array(
                        'comid' => $param['comid'],
                        'mid' => $id,
                        'password' => md5(md5($pswd) . config('admin.manager_password_halt')),
                        'addtime' => time(),
                        'pass' => $pswd,
                        'tel' => $param['tel'],
                        'dutyid' => $param['dutyid'],
                        'jobid' => $param['jobid'],
                    );
                    if($param['dutyid'] == 1){
                        $data['states'] = -1;
                    }
                    $mpmodel->addData($data);
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  僧侣修改]
     * @param ftype 2 法师不存在 3 法师已存在 4 用户不存在
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $memodel = new MemberModel;
        $model = new ManagerModel;
        $mcmodel = new ManagerCompanyModel;
        $mpmodel = new ManagerPasswordModel;
        try{
            $manager = $model->findData($param['tel'],'tel','id');
//            $memodel->editDataBy(['sex'=>$param['sex'],['bathday'=>$param['bathday']]],['tel'=>$param['tel']]);
            $mcmodel->editDataBy(['dutyid'=>$param['dutyid'],'jobid'=>$param['jobid']],['mid'=>$manager['id'],'comid'=>$param['comid']]);
            $mpmodel->editDataBy(['dutyid'=>$param['dutyid'],'jobid'=>$param['jobid']],['mid'=>$manager['id'],'comid'=>$param['comid']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        return admin_json(1,'修改成功');
    }
    /**
     * [getDetial  僧侣详情]
     * @param ftype 2 法师不存在 3 法师已存在 4 用户不存在
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getDetial()
    {
        $param = input('param.');
        $memodel = new MemberModel;
        $model = new ManagerModel;
        $mcmodel = new ManagerCompanyModel;
        $mdmodel = new ManagerDutyModel;
        $mjmodel = new ManagerJobModel;
        try{
            $manager = $model->findData($param['mid'],'id','id,tel,username');
            $member = $memodel->findData($manager['tel'],'tel','img,sex,bathday');
            $http = substr($member['img'],0,4);
            if($http != 'http'){
                $member['img'] = config('domain_name').$member['img'];
            }
            $manager['img'] = $member['img'];
            $manager['sex'] = $member['sex'];
            $manager['bathday'] = $member['bathday'];
            $mcdata = $mcmodel->findOne(['mid'=>$param['mid'],'comid'=>$param['comid']],'dutyid,jobid');
            if($mcdata['dutyid'] == 0){
                $mcdata['dutyid'] = 2;
            }
            $duty = $mdmodel->findData($mcdata['dutyid'],'id','duty');
            $manager['duty'] = $duty['duty'];
            $manager['dutyid'] = $mcdata['dutyid'];
            $manager['jobid'] = $mcdata['jobid'];
            if(empty($mcdata['jobid'])){
                $manager['job'] = '无';
            }else{
                $job = $mjmodel->findData($mcdata['jobid'],'id','job');
                $manager['job'] = $job['job'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',$manager);
    }
}